package 力扣.数学;

import org.junit.jupiter.api.Test;

public class 排列硬币441 {
    public int arrangeCoins(int n) {
        long sum = 0;
        int te = 0;
        int rowNum = 0;
        while (sum < n){
            te += 1;
            sum += te;
            if (sum <= n){
                rowNum++;
            }
        }
        return rowNum;
    }

    //等差数列+二分
    public int arrangeCoins2(int n) {
        if (n <= 1){
            return n;
        }
        long rowNum = 0;
         long l = 0L , r = n;
         while (l < r){
             long m = l + ((r - l) >> 1);
             long total = m + m*(m-1)/2;
             if (total <= n){
                 rowNum = m;
                 l = m + 1;
             }else{
                 r = m;
             }
         }
         return (int)rowNum;
    }

//    @Test
//    public void test1(){
//        System.out.println(1>>1);
//    }
}
